<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        .ta th, .ta td {
            border: 1px solid black;
            padding: 5px;
        }
        .p-img {
            height: 100px;
        }
    </style>
    <title>商品管理</title>
</head>
<?php
    if(!$_SESSION["user"]){
        // 未登录, 跳转登录页面
        header("location:/shop/login.html");
        return;
    }
?>
<body>
    <h2>商品管理</h2>
    <div>
        <span>
            欢迎:
            <span><?= $_SESSION["user"]["su_nickname"] ?></span>
        </span>
        <a href="/shop/logout.php">注销</a>
    </div>
    <hr>
    <div>
        <a href="/shop/add.html">添加商品</a>
    </div>
    <div>
        <?php
            require_once("productDao.php");
            // 当前页码
            $pageNum = empty($_REQUEST["pageNum"]) ? 1 : $_REQUEST["pageNum"];
            // 每页显示的数据条数
            $pageSize = 2;
            $result = selectByPage($pageNum, $pageSize);
            // print_r($result);

            echo "<div>总共 {$result['total']} 条数据 (共 {$result['pages']} 页)</div>";
            $arr = $result["data"];

            echo "<table class='ta' >";
            echo "<thead>";
            echo "<tr><th>编号</th><th>名称</th><th>简介</th><th>单价</th><th>单位</th><th>图片</th><th>操作</th></tr>";
            echo "</thead>";
            foreach ($arr as $p) {
                echo "<tr>";
                echo "<td>{$p['p_id']}</td>";
                echo "<td>{$p['p_name']}</td>";
                echo "<td>{$p['p_info']}</td>";
                echo "<td>{$p['p_price']}</td>";
                echo "<td>{$p['p_unit']}</td>";
                echo "<td><img class='p-img' src=\"{$p['p_image_url']}\" alt=\"暂无图片\" ></td>";
                echo "<td> <a href=\"/shop/removeById.php?id={$p['p_id']}\">删除</a> "
                ." <a href=\"/shop/findById.php?id={$p['p_id']}\">修改</a> </td>";
                echo "</tr>";
            }
            echo "</table>";

            echo "<div>";
            $preNum = $pageNum-1;
            $nextNum = $pageNum+1;
            if($preNum > 0){
                echo "<a href='/shop/productManage.php?pageNum={$preNum}'>上一页</a>";
            }
            if($nextNum <= $result['pages']){
                echo "<a href='/shop/productManage.php?pageNum={$nextNum}'>下一页</a>";
            }

            echo "<form action='/shop/productManage.php' method='get' >";
            echo "<input type='number' name='pageNum' min='1' max='{$result['pages']}' value='1' >";
            echo "<input type='submit' value='跳转到指定页面' >";
            echo "</form>";
            
            echo "</div>";
        ?>

        <!-- <script>
            // 转换成 json 数据, 可以直接在 js 中使用
            let data = <?= json_encode($result) ?>;
            console.log('data:', data);
        </script> -->
    
    </div>
</body>
</html>